﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenPop.Mime;
using OpenPop.Mime.Header;

namespace ggHD.Mail
{
    public static class MessageHdrHleper
    {
        public static string GetMessageId(this MessageHeader hdr)
        {
            return (!string.IsNullOrWhiteSpace(hdr.MessageId)) ? hdr.MessageId : hdr.From + "-" + hdr.DateSent;
        }
    }

    public class MailLoader : IDisposable
    {
        private PopWrapper _pop;
        public MailLoader(string host, int port, bool ssl, string user, string pass, IEnumerable<string> uids = null)
        {
            _pop = new PopWrapper(host, port, ssl, user, pass, uids);
        }

        public IDictionary<string, Message> LoadMessages(DateTime date, string from)
        {
            int depth = Math.Abs((date - DateTime.Today).Days) * 200;
            depth = depth > 0 ? depth : 200;
            return _pop.FindMsg(date, from, loadDepth: depth);
        }

        public void Dispose()
        {
            if (_pop != null)
                _pop.Dispose();
        }
    }
}
